
package robotlion.view;

import Tools.JDOMManager;
import java.awt.*;
import java.io.File;
import java.util.Hashtable;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import robotlion.controller.InfoIncendie;
import robotlion.controller.Simulateur;

/**
 *
 * @author Adrien Dos Reis
 */
public class FenetreRobotLion extends JPanel implements Observateur{
    
    Hashtable<Point, IncendiePanel> listeIncendie;
    Hashtable<String, RobotPanel> listeRobot;
    private Simulateur controller;
    private Image background;
    
    private JDOMManager myJDOM;

    public JDOMManager getMyJDOM() {
        return myJDOM;
    }
    
    public FenetreRobotLion()
    {
        super();
        controller = new Simulateur();
        listeRobot = new Hashtable<String, RobotPanel>();
        listeIncendie = new Hashtable<Point, IncendiePanel>();
        this.setLayout(null);  
        
        String nomFichier = "saveRobotLion.xml";
        JFileChooser selection = new JFileChooser(".");
        
        FileFilter imagesFilter = new FileNameExtensionFilter("type xml","xml");
        selection.setFileFilter(imagesFilter);
        
        selection.setSelectedFile(new File(nomFichier));
        
        int resultat = selection.showOpenDialog(null);
        if (resultat==JFileChooser.APPROVE_OPTION)
        {
            nomFichier = selection.getSelectedFile().getName();
        }

        //nom du fichier sans l'extension
        myJDOM = new JDOMManager(nomFichier.substring(0,nomFichier.lastIndexOf('.')));
        
    }
    
    @Override
    public void paintComponent(Graphics g) {
        g.drawImage(background, 0, 0, null);
    }

    public Simulateur getController() {
        return controller;
    }

    public void setController(Simulateur controller) {
        this.controller = controller;
    }
    
    public void setBackground(Image bg)
    {
        background = bg;
        Dimension size = new Dimension(bg.getWidth(null), bg.getHeight(null));
        this.setPreferredSize(size);
        this.setMinimumSize(size);
        this.setMaximumSize(size);
        this.setSize(size);
    }

    public void updateRobot(String nom, int x, int y)
    {
        RobotPanel theRobot;
        if(!listeRobot.containsKey(nom))
        {
            theRobot = new RobotPanel();
            listeRobot.put(nom, new RobotPanel());
        }
        theRobot = listeRobot.get(nom);
        this.add(theRobot);
        theRobot.setLocation(x, y);
        theRobot.setBounds(x*20, y*20, 20, 20);
        theRobot.repaint();
    }
    
    public void removeRobot(String nom)
    {
        RobotPanel remove = listeRobot.remove(nom);
        remove.setVisible(false);
        remove = null;
    }
    
     public void updateObs(Object info) {
        if(info.getClass() == InfoIncendie.class)
        {
            InfoIncendie ii = (InfoIncendie)info;
            Point inc = ii.position;
            IncendiePanel theFire;
            if(ii.isIncendie)
            {
                theFire = new IncendiePanel();
                this.listeIncendie.put(inc, theFire);
                this.add(listeIncendie.get(inc));
                theFire.setLocation(inc.x, inc.y);
                theFire.setBounds(inc.x*20, inc.y*20, 20, 20);
                theFire.repaint();
            }
            else
            {
                theFire = this.listeIncendie.remove(inc);
                if (theFire != null)
                {
                    this.remove(theFire);                   
                }
            }
        }
    }
}
